// pages/ucenter/teamLeader/becomeTl/becomeTl.js
var area = require('../../../../utils/area.js');
const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
let statusDic = {
  "0": "审核中",
  "1": "审核通过",
  "2": "已拒绝,点击重新提交"
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    regionShow: false,
    regionList: [],
    tlTypeDic: ["城市团长", "农村团长"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo.teamLeader) {
      userInfo.teamLeader.tlType += "" 
    }
    this.setData({
      userInfo: userInfo,
      mobile: userInfo.encodeMobile,
      regionList : area.areaList,
      ...userInfo.teamLeader,
      tlType: "0",
      edited: true
    });  
    
    if (userInfo.teamLeader) {
      this.refreshTL(this, userInfo)
      let that = this
      util.request(api.TeamLeaderGet, {userId: userInfo.teamLeader.id}, 'GET').then(res => {
        if (res.errno === 0) {
          console.log(res.data);
          userInfo.teamLeader = res.data
          that.refreshTL(that, userInfo)
        } else {
          console.log(333);
          userInfo.teamLeader = undefined
          that.refreshTL(that, userInfo)
        }
      });
    } else {
      that.refreshTL(that, userInfo)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTlTypeChange(event) {
    this.setData({
      tlType: event.detail
    });
  },
  showRegionSelect() {
    // this.setData({
    //   regionShow: true
    // })
    if (!this.data.edited) {
      return
    }
    let that = this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation']) {
          that.getTlLocation(that)
        } else {
          wx.authorize({
            scope: 'scope.userLocation',
            success () {
              that.getTlLocation()
            }
          })
        }
      }
    })
    
  },
  onRegionClose() {
    this.setData({
      regionShow: false
    })
  },
  onRegionConfirm(e) {
    this.setData({
      regionShow: false,
      regionCode: e.detail.values[e.detail.values.length - 1].code,
      regionName: e.detail.values.map(e => e.name).join(' ')
    })
    console.log(this.data.tl);
  },
  afterIdFRead(e) {
    let imgList = this.data.idBList
    if (imgList) {
      imgList.push(e.detail.file)
    } else {
      imgList = [e.detail.file];
    }
    this.setData({ 
      idFList: imgList
    })
  },
  afterIdBRead(e) {
    let imgList = this.data.idBList
    if (imgList) {
      imgList.push(e.detail.file)
    } else {
      imgList = [e.detail.file];
    }
    this.setData({ 
      idBList: imgList
    })
  },
  afterShopImgRead(e) {
    let imgList = this.data.shopImg
    if (imgList) {
      imgList.push(e.detail.file)
    } else {
      imgList = [e.detail.file];
    }
    this.setData({ 
      shopImg: imgList
    })
  },
  onSubmit(e) {  
    if (!this.data.edited) {
      return
    }
    let that = this  
    let req = this.data
    req.status = 0
    req.regionCode = 0
    
    Dialog.confirm({
      title: '提示',
      message: '是否提交申请入驻团长',
    }).then(() => {
      console.log(req);
      util.request(api.TeamLeaderSettled, req, 'POST').then(res => {
        if (res.errno === 0) {
          that.data.userInfo.teamLeader = res.data
          that.setData({
            status: 0,
            userInfo: that.data.userInfo,
            edited: false
          })
          that.refreshTL(that, that.data.userInfo)
        } else {
          Toast('提交失败，请稍后再试');
        }
        console.log(that.data);
      }).catch((err) => {
        console.log(err);
        Toast('提交失败，请稍后再试');
      });
    }).catch((err) => {
        // on cancel
        console.log(err);
    });
    
  },
  getTlLocation: (that) => {
    wx.getLocation({
      type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
      fail(res) {
        console.log(res);
      },
      success(res) {
        console.log(res);
        const latitude = res.latitude
        const longitude = res.longitude
        wx.chooseLocation({
          latitude,
          longitude,
          scale: 18,
          success: (res) => {
            that.setData({
              lat: res.latitude,
              lng: res.longitude,
              regionName: res.address,
              address: res.address + " " + res.name
            })
          }
        });
      }
    });
  },
  refreshTL(that, userInfo) {
  
    if (userInfo.teamLeader) {    
      let edited = !userInfo.teamLeader 
        || userInfo.teamLeader.status == 2    
      console.log(userInfo.teamLeader.tlType);
      that.setData({
        statusDesc : statusDic[userInfo.teamLeader.status + ''],
        edited : edited,
        tlType: userInfo.teamLeader.tlType + "",
      })
      wx.setStorageSync('userInfo', userInfo)
    } else {
      that.setData({
        id: undefined,
        statusDesc : undefined,
        edited : true,
        tlType: '0'
      })
    }
  }
})